<?php
namespace app\admin\controller;

use app\admin\validate\LoginValidate;
use app\common\AdminController;
use app\common\model\Lang as LM;
use app\common\model\System as SM;
use think\facade\Cache;
use think\facade\Session;
use app\common\exception\AdminException;

class Index extends AdminController
{
    /**
     * 自动验证
     * @var string
     */
    protected $validate = LoginValidate::class;

    /**
     * @return \think\response\View
     */
    public function index()
    {
        //echo password_hash('super888', PASSWORD_DEFAULT);die;
        $loginType = SM::getItem('login_type');
        return view('',[
            'login_type' => $loginType,
            'langs' => LM::select()
        ]);
    }

    /**
     * 登陆处理
     * @return \think\response\Json
     */
    public function login()
    {
        return $this->successRender();
    }

    /**
     * 欢迎页面
     * @return \think\response\View
     */
    public function welcome()
    {
        if ( ($lang_tag = $this->request->param('lang')) != '' ) {
            $lang_id = LM::where('tag', $lang_tag)->value('id');
            Session::set('admin.lang.id', $lang_id);
            Session::set('admin.lang.tag', $lang_tag);
        }

        return view('', [
            'system_menu'   => list_system_menu(),
            'langs'         => LM::select(),
            'use_lang'      => session('admin.lang.tag')
        ]);
    }

    /**
     * @return \think\response\View
     */
    public function main()
    {
        return view();
    }

    /**
     * [logout 退出登陆]
     * @return [type] [description]
     */
    public function logout()
    {
        Session::clear();
        return $this->successRender();
    }

    /**
     * [uploader 上传 ]
     * @return [type] [description]
     */
    public function uploader()
    {
        try {
            $info = filesystem_uploader(
                $this->request->param('dis', 'image', 'strip_tags'), 'file'
            );
            return $this->successRender($info);
        } catch (\Exception $ae) {
            throw new AdminException($ae->getMessage());
        }
    }

    /**
     * [uploaded 编辑器上传工具]
     * @return [type] [description]
     */
    public function uploaded()
    {
        try {
            $ueditorConfigFile = $this->app->getRootPath() . '/config.json';
            $ueditorConfigJson = json_decode(
                preg_replace(
                    "/\/\*[\s\S]+?\*\//",
                     "",
                      @file_get_contents($ueditorConfigFile)
                  ), true
             );
             $action = $this->request->param('action');
             switch( $action ) {
                 case 'config':
                    return json($ueditorConfigJson);
                 break;

                 case 'uploadimage':
                 case 'uploadscrawl':
                 case 'uploadvideo':
                 case 'uploadfile':
                    $info = filesystem_uploader('editor', 'upfile');
                    $info['state'] = 'SUCCESS';
                    return json($info);
                 break;
             }
             return $this->errorRender('请求方法不存在');
        } catch (\Exception $ae) {
            throw new AdminException($ae->getMessage());
        }
    }
}
